/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package colortest;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Robot;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

/**
 *
 * @author Nhat Lam
 */
public class panel extends JPanel implements MouseListener, MouseMotionListener{
    int mousex = -1, mousey = -1;
    boolean pressed = false;
    
    int r1 = 0, g1 = 0, b1 = 0, r2 = 0, g2= 0, b2 =0, r3 = 0, g3 = 0, b3 = 0;
    Random rd = new Random();
    Robot rb;
    public panel(){
        setSize(600, 600);
        addMouseListener(this);
        addMouseMotionListener(this);
        try {
            rb = new Robot();
        } catch (AWTException ex) {            
        }
    }
    @Override
    public void paintComponent(Graphics g){
        g.clearRect(0, 0, 600, 600);
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 600, 600);
        g.setXORMode(Color.BLACK);
        g.setColor(Color.red);
        g.fillRect(25, 75, 50, 50);
        g.setColor(Color.green);
        g.fillRect(50, 100, 50, 50);
        g.setColor(Color.red);
        g.fillOval(180, 80, 60, 60);
        g.setColor(Color.green);
        g.fillOval(170, 110, 70, 70);
        g.setColor(Color.blue);
        g.fillOval(210, 100, 50, 50);
//        g.setColor(Color.yellow);
//        g.fillRect(200, 200, 100, 100);
//        g.setColor(Color.blue);
//        g.fillRect(150, 250, 100, 100);
        
        g.setColor(new Color(r1, g1, b1));
        
        g.fillRect(400, 200, 100, 100);
        
        g.setColor(new Color(r2, g2, b2));
        
        g.fillRect(450, 250, 100, 100);
        g.setPaintMode();
        recaculatecolor();
        g.setColor(new Color(r3, g3, b3));
        
        g.fillRect(425, 400, 100, 100);
        
        if(pressed){
            g.setColor(Color.WHITE);
            g.drawLine(mousex, 0, mousex, 600);
            g.drawLine(0, mousey, 600, mousey);
            g.drawString(""+mousex, mousex + 5, 15);
            g.drawString(""+mousey, 5, mousey+15);
        }
        g.setColor(Color.WHITE);
        g.drawString("("+r1+", "+ g1 + ", "+ b1+")", 255, 240);
        g.drawString("("+r2+", "+ g2 + ", "+ b2+")", 255, 320);
        g.drawString("("+r3+", "+ g3 + ", "+ b3+")", 255, 420);
        
    }
    public void recaculatecolor(){
        r3 = r1^r2;
        if(r3 > 255)
            r3 -= 255;
        g3 = g1^g2;
        if(g3 > 255)
            g3 -= 255;
        b3 = b1^b2;
        if(b3 > 255)
            b3 -= 255;
    }
    private void renew(){
        r1 = rd.nextInt(255);
        r2 = rd.nextInt(255);
        r3 = rd.nextInt(255);
        g1 = rd.nextInt(255);
        g2 = rd.nextInt(255);
        g3 = rd.nextInt(255);
        b1 = rd.nextInt(255);
        b2 = rd.nextInt(255);
        b3 = rd.nextInt(255);        
    }
    @Override
    public void mouseClicked(MouseEvent me) {
//        System.out.println("Red = "+rb.getPixelColor(mousex, mousey).getRed());
//        System.out.println("Green = "+rb.getPixelColor(mousex, mousey).getGreen());
//        System.out.println("Blue = "+rb.getPixelColor(mousex, mousey).getBlue());
        
        renew();
        repaint();
    }

    @Override
    public void mousePressed(MouseEvent me) {        
    }

    @Override
    public void mouseReleased(MouseEvent me) {
        
    }

    @Override
    public void mouseEntered(MouseEvent me) {
        pressed = true;
    }

    @Override
    public void mouseExited(MouseEvent me) {
        
    }

    @Override
    public void mouseDragged(MouseEvent me) {
    }

    @Override
    public void mouseMoved(MouseEvent me) {
        mousex = me.getX();
        mousey = me.getY();        
        repaint();
    }
    
}
